<template>
  <div>
    <el-button type="primary" class="mb-4" @click="addLanguage">新增</el-button>
    <el-table :data="tableData">
      <el-table-column prop="id" label="ID" width="100" />
      <el-table-column prop="description" label="语言" align="center" />
      <el-table-column prop="locale" label="简称" align="center" />
      <el-table-column prop="name" label="翻译语言" align="translation" />

      <el-table-column prop="payTime" label="状态" align="center">
        <template #default="scope">
          <el-switch
            :width="58"
            v-model="scope.row.status"
            :active-value="1"
            :inactive-value="0"
            active-text="开启"
            inactive-text="关闭"
            :loading="switchLoading"
            inline-prompt
            :disabled="scope.row.disabled"
            @change="switchProcess(scope.row)"
          />
        </template>
      </el-table-column>

      <el-table-column prop="newTime" label="修改时间" align="center" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="handleEdit(scope.row)"
            v-if="scope.row.locale !== 'zh'"
          >
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      v-model="dialogVisible"
      :title="title"
      :before-close="handleClose"
      width="500px"
    >
      <el-form
        :model="languageParams"
        ref="languageForm"
        label-position="left"
        :rules="languageRules"
      >
        <el-form-item label="语言名称" prop="description" required>
          <el-input v-model.trim="languageParams.description" placeholder="请输入" />
        </el-form-item>

        <el-form-item label="语言简称" prop="locale" required>
          <el-input
            maxlength="3"
            v-model.trim="languageParams.locale"
            placeholder="请输入"
          />
        </el-form-item>
        <el-form-item label="翻译语言" prop="name" required>
          <el-input v-model.trim="languageParams.name" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="状态" prop="status" required>
          <el-switch
            :width="58"
            v-model="languageParams.status"
            :active-value="1"
            :inactive-value="0"
            inline-prompt
            active-text="显示"
            inactive-text="关闭"
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button @click="handleClose">取消</el-button>
          <el-button type="primary" @click="handleSure(languageForm)">
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup >
import { ref, reactive, onMounted } from "vue";
const { proxy } = getCurrentInstance();
const publicItem = ref({});
const dialogVisible = ref(false); //默认不显示弹出框
const tableData = ref([]);
const title = ref("新增");
const isAdd = ref(true); //默认是新增
const languageForm = ref();
const switchLoading = ref(false);
const languageParams = ref({
  name: "",
  locale: "",
  status: 1,
  description: "",
});
const languageRules = reactive({
  name:[
  {
      required: true,
      message: "请输入",
      trigger: "blur",
    },
  ],
  description: [
    {
      required: true,
      message: "请输入",
      trigger: "blur",
    },
    {
      pattern: /^[\u4e00-\u9fa5]+$/,
      message: "只能输入中文",
      trigger: "blur",
    },
  ],
  locale: [
    {
      required: true,
      message: "请输入",
      trigger: "blur",
    },
    {
      pattern: /^[A-Za-z]+$/,
      message: "只能输入英文",
      trigger: "blur",
    },
  ],
  status: [
    {
      required: true,
      message: "请选择",
      trigger: "blur",
    },
  ],
});

onMounted(() => {
  getList();
});
// 获取列表
const getList = () => {
  product.language
    .languageList({
      params: {
        status: 1,
      },
    })
    .then((res) => {
      res.data = res.data.map((code) => {
        return {
          ...code,
          disabled: code.id === 1 ? true : false,
          newTime:  proxy.DateTime(code.lastModified) ,
        };
      });
      tableData.value = res.data;
    });
};
// 编辑
const handleEdit = (rowItem) => {
  publicItem.value = rowItem;
  dialogVisible.value = true;
  isAdd.value = false;
  title.value = "编辑";

  languageParams.value.name = rowItem.name;
  languageParams.value.locale = rowItem.locale;
  languageParams.value.status = rowItem.status;
  languageParams.value.description = rowItem.description;
};
const addLanguage = () => {
  dialogVisible.value = true;
  title.value = "新增";
  isAdd.value = true;
};
// 取消
const handleClose = () => {
  proxy.resetForm("languageForm");
  isAdd.value = false;
  dialogVisible.value = false;
  languageParams.value.name = "";
  languageParams.value.locale = "";
  languageParams.value.status = 1;
  languageParams.value.description = "";
};
// 确定
const handleSure = (formEl) => {
  if (!formEl) return;
  formEl.validate((valid, fields) => {
    if (valid) {
      if (isAdd.value) {
        proxy.$modal.confirm("是否确认新增？").then(() => {
          product.language
            .languageAdd({
              data: languageParams.value,
            })
            .then((res) => {
              if (res.code === 200) {
                handleClose();
                getList();
                proxy.$modal.msgSuccess("新增成功");
                proxy.resetForm("languageForm");
              }
            });
        });
      } else {
        proxy.$modal.confirm("是否确认修改？").then(() => {
          product.language
            .languageUpdate({
              pathParams: {
                id: publicItem.value.id,
              },
              data: languageParams.value,
            })
            .then((res) => {
              if (res.code === 200) {
                handleClose();
                getList();
                proxy.$modal.msgSuccess("修改成功");
              }
            });
        });
      }
    }
  });
};
// 删除
const handleDelete = (rowItem) => {
  var newArray = [];
  newArray.push(rowItem.id);
  proxy.$modal.confirm("是否确认删除此数据类型？").then(() => {
    product.language
      .languageDeletebatch({
        data: {
          ids: newArray,
        },
      })
      .then((res) => {
        if (res.code === 200) {
          getList();
          proxy.$modal.msgSuccess("删除成功");
        }
      });
  });
};
const switchProcess = (rowItem) => {
  switchLoading.value = true;

  proxy.$modal
    .confirm("是否确认修改此数据类型？")
    .then(() => {
      product.language
        .languageUpdatestatus({
          pathParams: {
            id: rowItem.id,
          },
          params: {
            status: rowItem.status,
          },
        })
        .then((res) => {
          if (res.code === 200) {
            switchLoading.value = false;
            getList();
            proxy.$modal.msgSuccess(res.message);
          }
        });
    })
    .catch((err) => {
      switchLoading.value = false;
      if (rowItem.status) {
        return (rowItem.status = 0);
      } else {
        return (rowItem.status = 1);
      }
    });
};
</script>